# coding=utf-8
# author:xc
# datetime:2018/8/2 14:59

from dbConnection.MysqlConn import Mysql
from app import util
from . import admin
from flask import request, render_template


@admin.route('/admin/recharge/list.html', methods=['POST', 'GET'])
@util.admin_login
def admin_recharge_list():
    if request.method == "GET":
        mysql = Mysql()
        sql = "select * from recharge order by weight ASC, id DESC, status desc"
        result = mysql.getAll(sql, None)
        mysql.dispose()
        data = []
        code = util.CODE_EMPTY
        if result:
            data = result
            code = util.CODE_SUCCESS
        res = {
            'data': data,
            'code': code
        }
        return render_template('admin/store/store_recharge.html', res=res)


@admin.route('/admin/recharge/stop', methods=['POST'])
@util.admin_login
def admin_recharge_stop():
    rechargeId = request.values.get('rechargeId')
    mysql = Mysql()
    sql = "update recharge set status=0 where id='%s'" % rechargeId
    res = mysql.update(sql, None)
    mysql.dispose()
    if res > 0:
        data = {
            'status': 1
        }
    else:
        data = {
            'status': 0
        }
    return util.to_json(data)


@admin.route('/admin/recharge/start', methods=['POST'])
@util.admin_login
def admin_recharge_start():
    rechargeId = request.values.get('rechargeId')
    mysql = Mysql()
    sql = "update recharge set status=1 where id='%s'" % rechargeId
    res = mysql.update(sql, None)
    mysql.dispose()
    if res > 0:
        data = {
            'status': 1
        }
    else:
        data = {
            'status': 0
        }
    return util.to_json(data)


@admin.route('/admin/recharge/delete', methods=['POST'])
@util.admin_login
def admin_recharge_delete():
    rechargeId = request.values.get('rechargeId')
    mysql = Mysql()
    sql = "delete from recharge where id='%s' " % rechargeId
    res = mysql.delete(sql, None)
    mysql.dispose()
    if res > 0:
        data = {
            'status': 1
        }
    else:
        data = {
            'status': 0
        }
    return util.to_json(data)


@admin.route('/admin/recharge/edit.html', methods=['POST', 'GET'])
@util.admin_login
def admin_recharge_edit():
    if request.method == 'GET':
        rechargeId = request.args.get('rechargeId')
        mysql = Mysql()
        sql = "select * from recharge where id='%s'" % rechargeId
        result = mysql.getOne(sql, None)
        mysql.dispose()
        return render_template("admin/store/recharge_edit.html", res=result)
    else:
        try:
            name = request.values.get('name')
            property = request.values.get('property',default=0)
            num = request.values.get('num')
            price = request.values.get('price')
            stop_time = request.values.get('stop_time')
            initial_price = request.values.get('initial_price')
            rechargeId = request.values.get('rechargeId')
            weight = request.values.get('weight', 1)
            mysql = Mysql()
            sql = "update recharge set name='%s',property='%s',num='%s',price='%s',stopTime='%s', initial_price='%s', weight = '%s' " \
                  "where id='%s'" % (name, property, num, price, stop_time, initial_price, weight, rechargeId)
            result = mysql.update(sql, None)
            code = util.CODE_EMPTY
            mysql.dispose()
            if result > 0:
                code = util.CODE_SUCCESS
        except Exception as e:
            print e
            code = util.CODE_ERROR
        data = {
            'code': code
        }
        return util.to_json(data)


@admin.route('/admin/recharge/newly.html', methods=['POST', 'GET'])
@util.admin_login
def admin_recharge_newly():
    if request.method == "GET":
        return render_template('admin/store/recharge_newly.html')
    else:
        try:
            name = request.values.get('name')
            property = request.values.get('property',default=0)
            num = request.values.get('num')
            price = request.values.get('price')
            initial_price = request.values.get('initial_price')
            stop_time = request.values.get('stop_time', 0)
            mysql = Mysql()
            sql = "insert into recharge(name,property,num,price,stopTime,initial_price,addTime)VALUES ('%s','%s','%s'," \
                  "'%s','%s','%s',now())" % (name, property, num, price, stop_time, initial_price)
            result = mysql.update(sql, None)
            code = util.CODE_EMPTY
            mysql.dispose()
            if result > 0:
                code = util.CODE_SUCCESS
        except Exception as e:
            print e
            code = util.CODE_ERROR
        data = {
            'code': code
        }
        return util.to_json(data)
